package com.relytosoft.mizar.analyze.algorithm.base.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.relytosoft.dubhe.common.core.object.ResponseResult;
import com.relytosoft.mizar.analyze.algorithm.base.service.PersonService;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.DispositionReqDto;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.FaceGroupReqDto;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.FacePersonReqDto;
import com.relytosoft.mizar.analyze.base.entity.pojo.dto.FaceSearchReqDto;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 人脸库及人脸管理
 *
 * @Author SunLi
 * @Date: 2022/5/16 18:23
 * @Version 1.0
 */
@RestController
@RequestMapping("/algo")
public class PersonController {

    protected Logger log = LoggerFactory.getLogger(this.getClass());

    @Resource
    private PersonService personService;

    @PostMapping(value = "/group/add")
    public ResponseResult createGrouop(@RequestBody String bodys) {
        FaceGroupReqDto faceGroupReqDto = JSON.parseObject(bodys, FaceGroupReqDto.class);
        //参数验证
        String mes = this.validateGroup(faceGroupReqDto);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        String groupId = personService.createGroup(faceGroupReqDto);
        Map<String, Object> result = new HashMap<>(2);
        result.put("id", faceGroupReqDto.getGroupId());
        result.put("groupId", groupId);
        return ResponseResult.ok(result);
    }

    @PostMapping(value = "/group/delete")
    public ResponseResult deleteGrouop(@RequestBody String bodys) {
        FaceGroupReqDto faceGroupReqDto = JSON.parseObject(bodys, FaceGroupReqDto.class);
        //参数验证
        if (faceGroupReqDto == null || faceGroupReqDto.getGroupIds() == null || faceGroupReqDto.getGroupIds().size() == 0) {
            return ResponseResult.fail("参数异常！");
        }
        return personService.deleteGroup(faceGroupReqDto);
    }

    @PostMapping(value = "/person/add")
    public ResponseResult createPerson(@RequestBody String bodys) {
        FacePersonReqDto facePersonReqDto = JSON.parseObject(bodys, FacePersonReqDto.class);
        //参数验证
        String mes = this.validatePerson(facePersonReqDto);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        return personService.createPerson(facePersonReqDto);
    }

    @PostMapping(value = "/person/delete")
    public ResponseResult deletePerson(@RequestBody String bodys) {
        FacePersonReqDto facePersonReqDto = JSON.parseObject(bodys, FacePersonReqDto.class);
        //参数验证
        if (facePersonReqDto == null) {
            return ResponseResult.fail("参数异常！");
        }
        if (StrUtil.isEmpty(facePersonReqDto.getPersonId())) {
            return ResponseResult.fail("人脸ID不能为空！");
        }
        if (StrUtil.isEmpty(facePersonReqDto.getGroupId())) {
            return ResponseResult.fail("人脸库不能为空！");
        }
        return personService.deletePerson(facePersonReqDto);
    }

    @PostMapping(value = "/person/search")
    public ResponseResult searchPerson(@RequestBody String bodys) {
        FaceSearchReqDto faceSearchReqDto = JSON.parseObject(bodys, FaceSearchReqDto.class);
        //参数验证
        String mes = this.validateSearch(faceSearchReqDto);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        String result = personService.searchPerson(faceSearchReqDto);
        return ResponseResult.ok(JSON.parseArray(result));
    }

    @PostMapping(value = "/disposition/add")
    public ResponseResult createDisposition(@RequestBody String bodys, @RequestParam(name = "uploadUrl", required = false) String uploadUrl) {
        DispositionReqDto dispositionReqDto = JSON.parseObject(bodys, DispositionReqDto.class);
        //参数验证
        String mes = this.validateDisposition(dispositionReqDto);
        if (!StrUtil.isEmpty(mes)) {
            return ResponseResult.fail(mes);
        }
        String dispositionId = personService.createDisposition(dispositionReqDto, uploadUrl);
        Map<String, Object> result = new HashMap<>(2);
        result.put("id", dispositionReqDto.getId());
        result.put("dispositionId", dispositionId);
        return ResponseResult.ok(result);
    }

    @PostMapping(value = "/disposition/delete")
    public ResponseResult deleteDisposition(@RequestParam(name = "dispositionId") String dispositionId) {
        if (StrUtil.isEmpty(dispositionId)) {
            return ResponseResult.fail("任务ID不能为空！");
        }
        return personService.deleteDisposition(dispositionId);
    }

    private String validateGroup(FaceGroupReqDto faceGroupReqDto) {
        if (faceGroupReqDto == null) {
            return "参数异常！";
        }
        if (faceGroupReqDto.getVolume() == null) {
            return "容量不能为空！";
        }
        if (faceGroupReqDto.getVolume() <= 0) {
            return "容量值错误！";
        }
        return "";
    }

    private String validatePerson(FacePersonReqDto facePersonReqDto) {
        if (facePersonReqDto == null) {
            return "参数异常！";
        }
        if (StrUtil.isEmpty(facePersonReqDto.getImage())) {
            return "人脸图片不能为空！";
        }
        if (facePersonReqDto.getGroupIds() == null) {
            return "人脸所属库不能为空！";
        }
        if (facePersonReqDto.getGroupIds().size() == 0) {
            return "人脸所属库不能为空！";
        }
        return "";
    }

    private String validateSearch(FaceSearchReqDto faceSearchReqDto) {
        if (faceSearchReqDto == null) {
            return "参数异常！";
        }
        if (StrUtil.isEmpty(faceSearchReqDto.getFeature())) {
            return "人脸图片特征值不能为空！";
        }
        if (faceSearchReqDto.getTop() == null && faceSearchReqDto.getTop() <= 0) {
            return "查询条数不能为空！";
        }
        if (faceSearchReqDto.getThreshold() == null && faceSearchReqDto.getThreshold() <= 0) {
            return "相似度不能为空！";
        }
        if (faceSearchReqDto.getGroupIds() == null) {
            return "人脸库不能为空！";
        }
        if (faceSearchReqDto.getGroupIds().size() == 0) {
            return "人脸库不能为空！";
        }
        return "";
    }

    private String validateDisposition(DispositionReqDto dispositionReqDto) {
        if (dispositionReqDto == null) {
            return "参数异常！";
        }
        if (dispositionReqDto.getId() == null || dispositionReqDto.getId() <= 0) {
            return "参数异常！";
        }
        if (StrUtil.isEmpty(dispositionReqDto.getDeviceId())) {
            return "设备ID不能为空！";
        }
        Integer dispositionType = dispositionReqDto.getType();
        if (dispositionType == null) {
            return "布控类型参数异常！";
        }
        if (dispositionType == 1) {
            if (StrUtil.isEmpty(dispositionReqDto.getFeatureData())) {
                return "布控图片特征值不能为空！";
            }
        } else if (dispositionType == 2) {
            if (dispositionReqDto.getGroupIds() == null || dispositionReqDto.getGroupIds().size() == 0) {
                return "布控的人脸库不能为空！";
            }
        } else {
            return "布控类型参数异常！";
        }
        if (dispositionReqDto.getThreshold() == null || dispositionReqDto.getThreshold() < 0) {
            return "相似度参数异常！";
        }
        return "";
    }
}
